import { createElement } from "./utils.mjs";
import { LogicTerminal } from "./terminal.mjs";
import * as hdls from "../hdls/index.mjs";

/**
 * 获取核心库
 */
const coreLib = window["logicgatescore"];
if (!coreLib) {
    throw new ReferenceError("The core library of the application is undefined.");
}
// console.debug("The core library version:", coreLib.version);

/**
 * 定义电路仓库
 */
const ccRepo = (function () {
    const repo = new coreLib.tools.CircuitRepository();
    // 注册内建单元的HDL到缓存
    for (const hdl of coreLib.builtinUnits.hdls) {
        repo.builtinHDLCache.register(hdl.model, hdl);
    }
    // 注册内建单元的构造器到缓存
    for (const evalCtor of coreLib.builtinUnits.evalCtors) {
        repo.builtinEvalCtorCache.register(evalCtor.MODEL, evalCtor);
    }
    // 注册复合单元的HDL到缓存
    for (const key of Object.keys(hdls)) {
        const hdl = hdls[key];
        repo.compoundHDLCache.register(hdl.model, hdl);
    }
    return repo;
})();

/**
 * 模版
 */
const _template = (function () {
    const template = createElement("template");
    const styleElm = createElement("style", { "type": "text/css" }, [
        document.createTextNode(
            ":host{" +
            "display:block;" +
            "min-width:300px;" +
            "min-height:150px;" +
            "user-select:none;" +
            "overflow:hidden;" +
            "}"
        ),
        document.createTextNode(
            "#terminal-wrapper {" +
            "display:flex;" +
            "align-items:center;" +
            "align-items:center;" +
            "justify-content:center" +
            "}"
        )
    ]);
    template.content.appendChild(styleElm);
    template.content.appendChild(createElement("div", {
        "id": "terminal-wrapper",
    }));

    return template;
})();


/**
 * 逻辑电路应用
 */
class LogicWebApp extends HTMLElement {
    /**
     * 构造函数
     */
    constructor() {
        super()
        // 是否被聚焦
        this._focused = false;

        const templateContent = _template.content.cloneNode(true);
        // 创建内部根节点
        const shadowRoot = this.attachShadow({
            mode: "closed"
        });
        shadowRoot.appendChild(templateContent);
        const terminalWrapper = shadowRoot.querySelector("#terminal-wrapper");
        if (terminalWrapper === null) {
            throw new ReferenceError("terminalWrapper");
        }
        // 初始化终端
        const terminal = new LogicTerminal(800, 600);
        terminalWrapper.appendChild(terminal);

        // 构造APP
        const app = new coreLib.shell.LogicCLIApp(ccRepo);
        // 构造APP壳
        const appShell = new coreLib.shell.LogicCLIShell(terminal);
        appShell.lineLength = 96;
        appShell.lineCount = 32;
        // 输出内核版本号
        appShell.writeLog("core version:", `"${coreLib.version}"`);
        // 运行
        appShell.run(app);
    }

    /**
     * DOM挂载后的回调
     */
    connectedCallback() {
        
    }

    /**
     * DOM卸载后的回调
     */
    disconnectedCallback() {

    }
}

// 注册到WebComponent
customElements.define("logic-webapp", LogicWebApp);
